package com.ebs.md.fxmonitor.marketmonitor.config;

/**
 * Perforce Revision:
 *  $File: //depot/MarketData/fxmonitor/main/src/main/java/com/ebs/md/fxmonitor/marketmonitor/config/FlashingConfigImpl.java $
 *  $Revision: #19 $  $Change: 116168 $  $DateTime: 2012/10/24 11:29:22 $  $Author: Aviad.Yosef $
 */

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

import com.ebs.md.fxmonitor.marketmonitor.MarketMonitorConsts;
import com.ebsdr.fsp.ccc.configuration.IConfiguration;


public class FlashingConfigImpl implements IFlashingConfig, IConfigListener, MarketMonitorConsts
{
    final protected Logger logger = Logger.getLogger(getClass());
    final protected static String xsdPath = "/MonitoredMarketActivity/Flashings/Flashing";

    private long lifeTimeThreshold;
	private Map<String, Integer>  pipCountThresholdMap;
	private int  numberOfFlashesThreshold;
	private long timeFrameThreshold;
	private int  numberOfInvolvedDealsThreshold;
	
	public FlashingConfigImpl()
	{
		pipCountThresholdMap = new HashMap<String, Integer>();
	}
	
	@Override
	public String getName() {
		return BASIC_CONFIG_NAME;
	}

	public void onConfig(IConfiguration configuration)
	{
		lifeTimeThreshold              = configuration.getLong(xsdPath + "/@interruptTime",        EVENT_FLASHING_LIFE_TIME_THRESHOLD);
		numberOfFlashesThreshold       = configuration.getInt (xsdPath + "/@numberOfPriceFlashes", EVENT_FLASHING_NUMBER_OF_FLASHES_THRESHOLD);
		timeFrameThreshold             = configuration.getLong(xsdPath + "/@timeFrame",            EVENT_FLASHING_TIME_FRAME_THRESHOLD);
		numberOfInvolvedDealsThreshold = configuration.getInt (xsdPath + "/@numberOfDeals",        EVENT_FLASHING_NUMBER_OF_INVOLVED_DEALS_THRESHOLD);
		setPipsCountList(configuration, xsdPath);
		
		logger.info( getConfigInfo() );
	}

	@Override
	public String getConfigInfo() {
		StringBuilder sb = new StringBuilder("FlahsingConfigImpl: ");
		
		sb.append( " lifeTime: "              + lifeTimeThreshold);
		sb.append(", numberOfFlahshes: "      + numberOfFlashesThreshold);
		sb.append(", numberOfInvoldedDeals: " + numberOfInvolvedDealsThreshold);
		sb.append(", timeFrame: "             + timeFrameThreshold);
		sb.append(", pipsCount: "             + pipCountThresholdMap);

		return sb.toString();
	}

	private void setPipsCountList(IConfiguration configuration, String xsdPath) 
	{
		pipCountThresholdMap.clear(); 

		List<String> ccyPairList = configuration.getList(xsdPath + "/CurrencyPair/@name");
		int numOfCurrencyPair = ccyPairList.size();
		
		for (int index = 1; index <= numOfCurrencyPair; index++)
		{
			String  ccyPairName               = configuration.getString(xsdPath + "/CurrencyPair[" + index + "]/@name");
			Integer ccyPairpipsDeltaThreshold = configuration.getInt   (xsdPath + "/CurrencyPair[" + index + "]/@flashThreshold" , EVENT_FLASHING_PIP_COUNT_THRESHOLD);			
			
			try {
				pipCountThresholdMap.put(ccyPairName, ccyPairpipsDeltaThreshold);
			}
			catch (NumberFormatException e){
				logger.error("NumberFormatException in setOffMarketCurrencyPairPips::pipsDeltaThreshold: " + ccyPairpipsDeltaThreshold); 				
			}
		}		
	}


	//getters
	@Override
	public long getLifeTimeThreshold() {
		return lifeTimeThreshold;
	}
		
	@Override
	public int getPipCountThreshold(String shortIsoPairName) {
		Integer res = pipCountThresholdMap.get(shortIsoPairName);

		if (res == null)
		{
			res = EVENT_FLASHING_PIP_COUNT_THRESHOLD;
		}
		
		return res; 
	}

	@Override
	public Set<String> getPairConfiguredWithPipCountThreshold() {
		return pipCountThresholdMap.keySet();
	}

	@Override
	public int getNumberOfFlashesThreshold() {
		return numberOfFlashesThreshold;
	}

	@Override
	public long getTimeFrameThreshold() {
		return timeFrameThreshold;
	}

	@Override
	public int getNumberOfInvolvedDealsThreshold() {
		return numberOfInvolvedDealsThreshold;
	}

}
